package com.ruoyi.config;

import com.ruoyi.interceptor.AuthInterceptor;
import com.ruoyi.server.MyWebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
@EnableWebSocket
//接线员（WebSocketConfig）：负责接听来电并转接给客服
public class WebSocketConfig implements WebSocketConfigurer {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
//     implements WebSocketConfigurer要实现这个
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler(), "/ws/endpoint") // 设置接听路径为/ws（类似电话号码）
                .addInterceptors(new AuthInterceptor())
                .setAllowedOrigins("*");                // 允许所有人拨打这个号码（实际需限制）
    }

    @Bean
    public WebSocketHandler webSocketHandler() {
        return new MyWebSocketHandler();// 指定客服人员
    }
}